namespace hjc {

	export abstract class View extends eui.Component {

		protected $viewType: string;
		protected $times: number = 0;
		protected $mediators: Mediator[] = [];
		protected $skinFile: string = null;
		private $align: AlignOptions;
		protected $configs: ViewConfigs = {
			showMode: ShowMode.NONE, hideMode: HideMode.NONE, centered: false,
		};

		protected createChildren(): void {
			super.createChildren();
			this._configurate();
			this.created();
		}

		private _configurate(): void {
			this.init();
			if (!isSkinView(this)) this.skinName = (!!this.$skinFile) ? this.$skinFile : `skins.${egret.getQualifiedClassName(this)}`;
			if (this instanceof Scene || !!this.$configs.centered) ui.setCenter(this);
			if (this instanceof Panel || this instanceof Dialog) {
				if (Math.notNegtive(this.configs.backAlpha)) {
					this.backRect = ui.setFull(ui.newRect({ name: `${this.name}[蒙板]`, alpha: this.configs.backAlpha, color: this.configs.backColor }));
					if (this.configs.backClose) {
						this.backRect.touchEnabled = true;
						this.backRect.addEventListener(egret.TouchEvent.TOUCH_TAP, () => (<Function>this['hide'])(), this);
					} else this.backRect.touchEnabled = false;
				}
				if (this.btnClose instanceof hjc.Button) this.btnClose.setClicked(() => (<Function>this['hide'])());
			}
			if (!!this.$align) ui.setAlign(this, this.$align);
		}

		public get first(): boolean { return this.$times === 1; }
		public get times(): number { return this.$times; }
		public get configs(): ViewConfigs { return this.$configs; }
		public get mediators(): Mediator[] { return this.$mediators; }
		
		protected init(): void { }
		protected addMediator(...mediatorClass: (new (v?: View) => Mediator)[]): void {
			let mediators = mediatorClass.map(m => new m(this));
			if (isEmpty(mediatorClass)) {
				let medName = egret.getQualifiedClassName(this) + 'Mediator';
				let medClass = egret.getDefinitionByName(medName);
				if (!medClass) { hjc.warn(`view's default mediator not found: '${medName}'`); return; }
				else mediators.push(new medClass(this) as Mediator);
			}
			this.$mediators.push(...mediators);
			game.manager.mediator.add(...mediators);
		}
		protected front(): void { }
		protected created(): void { }
		protected displayed(...params: any[]): void { }
		protected quit(): void { }

		protected setAlign(options: AlignOptions): void {
			this.$align = options;
		}
	}
}